package Dwarf;

public class DwarfSimulator
{
    private static int MAX_DAY_LIGHT_ITERATION = 4;
    private static int MAX_SPARE_TIME_ITERATION = 3;

    private int dayLightIteration;
    private int spareTimeIteration;

    private Dwarf currentDwarf;

    public DwarfSimulator(DwarfType type)
    {
        SetDwarf(type);
        dayLightIteration = MAX_DAY_LIGHT_ITERATION;
        spareTimeIteration = MAX_SPARE_TIME_ITERATION;
    }

    public void SetDwarf(DwarfType type)
    {
        switch (type)
        {
            case WARRIOR:
                currentDwarf = new DwarfWarrior();
                break;
            case FARMER:
                currentDwarf = new DwarfFarmer();
                break;
            case CRAFTSMAN:
                currentDwarf = new DwarfCraftsman();
                break;
            case MINER:
                currentDwarf = new DwarfMiner();
                break;
        }
    }

    public void ExecuteCommand(String commandValue)
    {
        System.out.print(currentDwarf.GetType() + ": ");
        if (commandValue.equals("right")) {currentDwarf.Right();}
        else if (commandValue.equals("left")) {currentDwarf.Left();}
        else if (commandValue.equals("forward")) {currentDwarf.Forward();}
        else if (commandValue.equals("backward")) {currentDwarf.Backward();}
        else if (commandValue.equals("say")) {currentDwarf.Say();}
        else if (commandValue.equals("go")) {currentDwarf.Go();}
        else if (commandValue.equals("stop")) {currentDwarf.Stop();}
        else if (commandValue.equals("attack")) {currentDwarf.Attack();}
        else if (commandValue.equals("work")) {currentDwarf.Work();}
        else
        {
            System.out.println("Command: " + commandValue);
        }
    }

    public boolean ConditionIsTrue(String conditionVariable)
    {
        boolean conditionIsTrue = false;
        if (conditionVariable.equals("dayLight"))
        {
            dayLightIteration = LoopIteration(dayLightIteration, MAX_DAY_LIGHT_ITERATION);
            conditionIsTrue = (dayLightIteration > 0);
        }
        else if (conditionVariable.equals("haveSpareTime"))
        {
            spareTimeIteration = LoopIteration(spareTimeIteration, MAX_SPARE_TIME_ITERATION);
            conditionIsTrue = (spareTimeIteration > 0);
        }
        return conditionIsTrue;
    }

    private int LoopIteration(int iteration, int maxValue)
    {
        iteration--;
        iteration = (iteration < 0) ? maxValue - 1 : iteration;
        return iteration;
    }

}
